<?php
namespace App\Http\Controllers\Post;

use App\Http\Controllers\Controller;
use App\Jobs\SendCommentMsgJob;
use App\Jobs\SendPraiseMsgJob;
use App\Jobs\SendSystemMsgJob;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Image\ImagesDecorator;
use App\Libraries\Decorator\Post\PostHiddenDecorator;
use App\Libraries\Decorator\Post\IsPraiseDecorator;
use App\Libraries\Decorator\Tool\CutStringDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Decorator\Tool\IsFavDecorator;
use App\Libraries\Decorator\User\UserPointDecorator;
use App\Libraries\Easemob\Easemob;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Libraries\Tools\Point;
use App\Libraries\Tools\Tools;
use App\Models\Config\Config;
use App\Models\HongBao\HongBao;
use App\Models\HongBao\HongBaoTemplate;
use App\Models\Msg\MsgComment;
use App\Models\Msg\MsgPraise;
use App\Models\Post\Post;
use App\Models\Post\PostComment;
use App\Models\Post\PostFav;
use App\Models\Post\PostPraise;
use App\Models\Post\PostPurchase;
use App\Models\Post\PostReport;
use App\Models\Post\PostSta;
use App\Models\Tool\BadWord;
use App\Models\Tool\District;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use OSS\OssClient;
use OSS\Core\OssException;


class PostController extends Controller{

    protected $post;
    protected $cover_decorator;
    protected $image_decorator;
    protected $is_end_decorator;
    protected $cut_string_decorator;
    protected $post_hidden_decorator;
    protected $isFavDecorator;
    protected $isPraiseDecorator;

    public function __construct(Post $post, CoverDecorator $cover,
                                ImagesDecorator $image,
                                IsEndDecorator $is_end,
                                CutStringDecorator $cut_string,
                                PostHiddenDecorator $post_hidden,
                                IsFavDecorator $isFavDecorator, IsPraiseDecorator $isPraiseDecorator){
        $this->post = $post;
        $this->cover_decorator = $cover;
        $this->image_decorator = $image;
        $this->is_end_decorator = $is_end;
        $this->cut_string_decorator = $cut_string;
        $this->post_hidden_decorator = $post_hidden;
        $this->isFavDecorator = $isFavDecorator;
        $this->isPraiseDecorator = $isPraiseDecorator;
    }


    /**
     * 帖子列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function posts(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
            'category' => 'in:0,1,2,3,4,5,12',//0-全部 1-水疗 2-KTV 3-夜总会 4-酒吧 5-沐足 12-其他
            'city_id' => 'integer',
            'district_id' => 'integer',
            'user_id' => 'required_if:type,7|integer',
            'type' => 'in:0,1,2,3,4,5,6,7',//0-最新 1-已购买 2-收藏 3-离我最近 4-最热 5-关键字搜索 6-关注 7-特定用户
            'keyword' => 'required_if:type,5|string',
            'lng' => 'required_if:type,3|numeric',
            'lat' => 'required_if:type,3|numeric',
            'shop_ids' => 'json',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'category.in' => '10002',
            'city_id.integer' => '10002',
            'district_id.integer' => '10002',
            'user_id.required_if' => '10001',
            'user_id.integer' => '10002',
            'type.in' => '10002',
            'keyword.required_if' => '10001',
            'keyword.string' => '10002',
            'lng.required_if' => '10001',
            'lng.numeric' => '10002',
            'lat.required_if' => '10001',
            'lat.numeric' => '10002',
            'shop_ids.json' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->post->addDecorator($this->cover_decorator);
        $this->post->addDecorator($this->image_decorator);
        $this->post->addDecorator($this->is_end_decorator);
//        $this->post->addDecorator($this->post_hidden_decorator);
        $this->post->addDecorator($this->isFavDecorator);
        $this->post->addDecorator($this->isPraiseDecorator);
        $this->post->addDecorator(new UserPointDecorator());
        $data = $this->post->getPosts(Auth::id(), $request);
        $data['free_key'] = '0';//0-免费 1-收费
        return $this->sendSuccessResponse($data);
    }

    /**
     * 帖子详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function post($id){
        $validator = Validator::make([
            'id' => $id,
        ], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->post->addDecorator($this->cover_decorator);
        $this->post->addDecorator($this->image_decorator);
//        $this->post->addDecorator($this->post_hidden_decorator);
        $this->post->addDecorator($this->isFavDecorator);
        $this->post->addDecorator($this->isPraiseDecorator);
        $this->post->addDecorator(new UserPointDecorator());
        $data = $this->post->detail($id, Auth::id());
        if(empty($data)){
            return $this->sendFailResponse(500, 50001);
        }
        return $this->sendSuccessResponse($data);
    }

    /**
     * 新增帖子
     * @param Request $request
     * @param PostSta $sta
     * @param BadWord $badWord
     * @return \Illuminate\Http\JsonResponse
     */
    public function addPost(Request $request, PostSta $sta, BadWord $badWord){
//        Log::debug(\GuzzleHttp\json_encode($request->all()));
        $validator = Validator::make($request->all(), [
            'post' => 'required|string',
            'images.*' => 'image',
            'category' => 'integer',
            'price' => 'numeric',
            'repost_id' => 'integer',
            'province_id' => 'integer',
            'city_id' => 'required|integer',
            'district_id' => 'integer',
            'shop_id' => 'integer',
            'consumed_at' => 'required_unless:price,0|string|max:10',
            'shop' => 'required_unless:price,0|string|max:50',
            'address' => 'required_unless:price,0|string|max:200',
            'longitude' => 'required_unless:price,0|numeric',
            'latitude' => 'required_unless:price,0|numeric',
            'contact' => 'required_unless:price,0|string|max:100',
            'consumption' => 'required_unless:price,0|string|max:100',
        ], [
            'post.required' => '10001',
            'post.string' => '10002',
            'images.*.image' => '10003',
            'price.numeric' => '10002',
            'category.integer' => '10002',
            'city_id.required' => '10001',
            'province_id.integer' => '10002',
            'city_id.integer' => '10002',
            'district_id.integer' => '10002',
            'repost_id.integer' => '10002',
            'shop_id.integer' => '10002',
            'consumed_at.required_unless' => '10001',
            'consumed_at.integer' => '10002',
            'shop.required_unless' => '10001',
            'shop.string' => '10002',
            'shop.max' => '10002',
            'address.required_unless' => '10001',
            'address.string' => '10002',
            'address.max' => '10002',
            'longitude.required_unless' => '10001',
            'longitude.numeric' => '10002',
            'latitude.required_unless' => '10001',
            'latitude.numeric' => '10002',
            'contact.required_unless' => '10001',
            'contact.string' => '10002',
            'contact.max' => '10002',
            'consumption.required_unless' => '10001',
            'consumption.string' => '10002',
            'consumption.max' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        if(!empty($request['post'])){
            $request['post'] = Tools::removeXSS($request['post']);
        }
        if(!empty($request['consumed_at'])){
            $request['consumed_at'] = Tools::removeXSS($request['consumed_at']);
        }
        if(!empty($request['shop'])){
            $request['shop'] = Tools::removeXSS($request['shop']);
        }
        if(!empty($request['address'])){
            $request['address'] = Tools::removeXSS($request['address']);
        }
        if(!empty($request['contact'])){
            $request['contact'] = Tools::removeXSS($request['contact']);
        }
        if(!empty($request['consumption'])){
            $request['consumption'] = Tools::removeXSS($request['consumption']);
        }

        if($badWord->checkBadWord($request['post']) != 200){
            return $this->sendFailResponse(400, 40005);
        }
        $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
        $bucket = env('ALIYUN_BUCKET');
        $images = [];

        $this->post->user_id = Auth::id();
        $this->post->post = $request['post'];
        $this->post->category = empty($request['category'])?12:$request['category'];
        $this->post->status = 1;
        $this->post->province_id = $request['province_id']??0;
        $this->post->city_id = $request['city_id'];
        $this->post->district_id = $request['district_id']??0;
        $this->post->shop_id = $request['shop_id']??0;
        if(!empty($request['images'])){
            foreach($request->file('images') as $v){
                if($v->isValid()){
                    try{
                        $object = 'image/'.uniqid().'.'.$v->extension();
                        $ossClient->uploadFile($bucket, $object, $v->getFileInfo());
                    }catch (OssException $e){
                        return $this->sendFailResponse(500, 40003);
                    }
                    $images[] = $object;
                }
            }
        }
        if(!empty($images)){
            $this->post->images = json_encode($images);
        }
        if(!empty($request['repost_id'])){
            $this->post->repost_id = $request['repost_id'];
            $sta->where('post_id', $request['repost_id'])->increment('repost');
        }
        $this->post->price = empty($request['price'])?0:$request['price'];
        $this->post->consumed_at = empty($request['consumed_at'])?'':$request['consumed_at'];
        $this->post->shop = empty($request['shop'])?'':$request['shop'];
        $this->post->address = empty($request['address'])?'':$request['address'];
        $this->post->longitude = empty($request['longitude'])?0:$request['longitude'];
        $this->post->latitude = empty($request['latitude'])?0:$request['latitude'];
        $this->post->contact = empty($request['contact'])?'':$request['contact'];
        $this->post->consumption = empty($request['consumption'])?'':$request['consumption'];
        $this->post->posted_at = time();
        $this->post->save();
        $sta->post_id = $this->post->id;
        $sta->save();
        (new Point())->addPostPoint(Auth::id(), $this->post->id);
        return $this->sendSuccessResponse(['post_id' => $this->post->id]);
    }

    /**
     * 帖子评论列表
     * @param Request $request
     * @param PostComment $comment
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function comments(Request $request, PostComment $comment, $id){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $comment->addDecorator($this->is_end_decorator);
        $comment->addDecorator($this->cover_decorator);
        $comment->addDecorator(new UserPointDecorator());
        $data = $comment->getComments($request['page'], $request['size'], $id);
        foreach($data['list'] as &$v){
            $v['sub_comments'] = $comment->getSubComments(1, 99999999, $v['id']);
//            $v['sub_comment_count'] = $comment->getSubCommentCount($v['id']);
        }
        return $this->sendSuccessResponse($data);
    }

    /**
     * 帖子评论详情
     * @param Request $request
     * @param PostComment $comment
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function comment(Request $request, PostComment $comment, $id){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $comment->addDecorator($this->is_end_decorator);
        $comment->addDecorator($this->cover_decorator);
        $data = $comment->getComment($id);
        if(empty($data)){
            return $this->sendFailResponse(404, 40400);
        }
        $data['sub_comments'] = $comment->getSubComments($request['page'], $request['size'], $id);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 添加帖子评论
     * @param Request $request
     * @param PostComment $comment
     * @param PostSta $sta
     * @param BadWord $badWord
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function addComment(Request $request, PostComment $comment, PostSta $sta, BadWord $badWord, $id){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'comment' => 'required|string|max:500',
            'comment_id' => 'required_with:reply_user_id|integer|min:1',
            'reply_user_id' => 'integer|min:1',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'comment.required' => '10001',
            'comment.string' => '10002',
            'comment.max' => '10002',
            'comment_id.required_with' => '10001',
            'comment_id.integer' => '10002',
            'comment_id.min' => '10002',
            'reply_user_id.required_with' => '10001',
            'reply_user_id.integer' => '10002',
            'reply_user_id.min' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        if(!empty($request['comment'])){
            $request['comment'] = Tools::removeXSS($request['comment']);
        }
        if($badWord->checkBadWord($request['comment']) != 200){
            return $this->sendFailResponse(400, 40005);
        }

        $post_data = $this->post->find($id);
        if(empty($post_data['status'])){
            return $this->sendFailResponse(400, 40001);
        }

        if(!empty($request['comment_id'])){
            $main_comment = $comment->where('id', $request['comment_id'])->firstOrFail();
        }

        $comment->post_id = $id;
        $comment->user_id = Auth::id();
        $comment->comment = $request['comment'];
        $comment->comment_id = $request['comment_id']??0;
        $comment->reply_user_id = $request['reply_user_id']??0;
        $comment->commented_at = time();
        $comment->save();

        $sta->where('post_id', $id)->increment('comment');

        (new Point())->addCommentPoint(Auth::id(), $id, $post_data->user_id, $comment->id);
        //发送环信通知--通知帖子作者
        if($post_data->user_id != Auth::id()){
            dispatch(new SendCommentMsgJob(
                    Auth::id(),
                    $id,
                    [$post_data->user_id],
                    $comment->id,
                    $comment->comment_id?$comment->comment_id:$comment->id,
                    Msg::replay_post_msg(),
                    ConstVar::$IM_MSG_COMMON
                )
            );
        }
        //发送环信通知--通知主评论作者
        if(!empty($main_comment)){
            if($main_comment->user_id != Auth::id() && $main_comment->user_id != $request['reply_user_id']){
                dispatch(new SendCommentMsgJob(
                        Auth::id(),
                        $id,
                        [$main_comment->user_id],
                        $comment->id,
                        $comment->comment_id,
                        Msg::replay_comment_msg(),
                        ConstVar::$IM_MSG_COMMON
                    )
                );
            }
        }
        //发送环信通知--通知子评论作者
        if(!empty($request['reply_user_id']) && $request['reply_user_id'] != Auth::id()){
            dispatch(new SendCommentMsgJob(
                    Auth::id(),
                    $id,
                    [$request['reply_user_id']],
                    $comment->id,
                    $comment->comment_id,
                    Msg::replay_comment_msg(),
                    ConstVar::$IM_MSG_COMMON
                )
            );
        }

        return $this->sendSuccessResponse([]);
    }

    /**
     * 点赞列表
     * @param Request $request
     * @param PostPraise $postPraise
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function praises(Request $request, PostPraise $postPraise, $id){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        //todo 验证 post

        $postPraise->addDecorator($this->is_end_decorator);
        $postPraise->addDecorator($this->cover_decorator);
        $data = $postPraise->getPraises($request['page'], $request['size'], $id);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 帖子点赞
     * @param $id
     * @param PostPraise $praise
     * @param PostSta $sta
     * @param MsgPraise $msgPraise
     * @return \Illuminate\Http\JsonResponse
     */
    public function praise($id, PostPraise $praise, PostSta $sta, MsgPraise $msgPraise){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $post_data = $this->post->find($id);
        if(empty($post_data['status'])){
            return $this->sendFailResponse(400, 40001);
        }

        $praise_data = $praise->firstOrCreate(['post_id'=>$id, 'user_id'=>Auth::id()]);
        $praise_data->status = intval($praise_data->status XOR 1);
        $praise_data->save();

        if($praise_data->status){
            $sta->where('post_id', $id)->increment('praise');

            //发送环信通知
            if($post_data->user_id != Auth::id()){
                dispatch(new SendPraiseMsgJob($post_data->user_id, Msg::praise_msg()));
//                Easemob::singleton()->sendText(5, 'users', [$post_data->user_id], Msg::praise_msg(), ['user_id'=>5, 'type'=>0]);

                $msgPraise->firstOrCreate([
                    'from_id' => Auth::id(),
                    'to_id' => $post_data->user_id,
                    'post_id' => $id,
                    'praise_id' => $praise_data->id,
                ]);
            }


        }else{
            $sta->where('post_id', $id)->decrement('praise');
        }
        return $this->sendSuccessResponse(['status'=>$praise_data->status]);
    }

    /**
     * 添加浏览量
     * @param $id
     * @param PostSta $statistic
     * @return \Illuminate\Http\JsonResponse
     */
    public function addView($id, PostSta $statistic){
        $validator = Validator::make(
            ['id' => $id], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        //todo 验证 post

        $statistic->where('post_id', $id)->increment('view');
        return $this->sendSuccessResponse([]);
    }

    /**
     * 用红包购买
     * @param $id
     * @param PostPurchase $purchase
     * @param HongBao $hong_bao
     * @return \Illuminate\Http\JsonResponse
     */
    public function buyWithHongBao($id, PostPurchase $purchase, HongBao $hong_bao){
        $user_id = Auth::id();
        $time = time();
        $validator = Validator::make(['post_id'=>$id], [
            'post_id' => 'required|integer',
        ], [
            'post_id.required' => '10001',
            'post_id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->post->addDecorator($this->cover_decorator);
        $this->post->addDecorator($this->image_decorator);
        $post_data = $this->post->detail($id, $user_id);
        if(empty($post_data)){//检查帖子状态
            return $this->sendFailResponse(400, 40001);
        }
        $purchase_data = $purchase->where('user_id', $user_id)->where('post_id', $id)->first();
        if(!empty($purchase_data)){//检查购买状态
            return $this->sendSuccessResponse($post_data);
        }
        $hong_bao_data = $hong_bao->where('user_id', $user_id)
            ->where('type', 1)
            ->where('status', 0)
            ->where('end_at', '>=', $time)
            ->orderBy('end_at', 'asc')
            ->first();
        if(empty($hong_bao_data)){//检查红包情况
            return $this->sendFailResponse(500, 50003);
        }
        DB::beginTransaction();
        try{
            $hong_bao_data->status = 1;
            $hong_bao_data->save();
            $purchase->user_id = $user_id;
            $purchase->post_id = $id;
            $purchase->status = 1;
            $purchase->save();
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500, 50000);
        }
        return $this->sendSuccessResponse($post_data);
    }

    /**
     * 收藏帖子
     * @param $id
     * @param PostFav $fav
     * @return \Illuminate\Http\JsonResponse
     */
    public function fav($id, PostFav $fav){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $user_id = Auth::id();
        //todo 检查帖子状态
        $fav_data = $fav->firstOrCreate(['user_id'=>$user_id, 'post_id'=>$id]);
        $fav_data->status = intval($fav_data->status XOR 1);
        $fav_data->save();
        return $this->sendSuccessResponse(['status'=>$fav_data->status]);
    }

    /**
     * 帖子举报
     * @param $id
     * @param PostReport $postReport
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function report($id, PostReport $postReport, Request $request){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'reason' => 'string|max:200',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'reason.string' => '10002',
            'reason.max' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $user_id = Auth::id();
        //todo 检查帖子状态
        $report = $postReport->firstOrCreate([
            'user_id' => $user_id,
            'post_id' => $id,
            'status'  => 0,
        ]);
        $report->reason = empty($request['reason'])?'':$request['reason'];
        $report->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 冻结帖子
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function disablePost($id){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $user = Auth::user();
        $post = $this->post->where('id', $id)->first();
        if(empty($user->is_admin) && $post->user_id != $user->user_id){
            return $this->sendFailResponse(400, 40101);
        }
        DB::beginTransaction();
        try{
            $this->post->where('id', $id)->update(['status'=>0]);
            $point = new Point();
            $point->setStatus($post->user_id, $id, 1, ConstVar::$POINT_STATUS_CANCEL);
            $point->resum($post->user_id);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500, 50000);
        }


        if($post->user_id != $user->user_id){
            $msg = Msg::disable_post($post->created_at->toDateTimeString());
            dispatch(new SendSystemMsgJob([$post->user_id], $msg, ConstVar::$IM_MSG_COMMON));
        }
        return $this->sendSuccessResponse([]);
    }

    /**
     * 冻结评论
     * @param $id
     * @param PostComment $postComment
     * @param PostSta $postSta
     * @return \Illuminate\Http\JsonResponse
     */
    public function disableComment($id, PostComment $postComment, PostSta $postSta){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $user = Auth::user();
        $comment = $postComment->where('id', $id)->firstOrFail();
        if(empty($user->is_admin) && $comment->user_id != $user->user_id){
            return $this->sendFailResponse(400, 40101);
        }
        if($comment->status == 0){
            return $this->sendSuccessResponse([]);
        }
        DB::beginTransaction();
        try{
            $postComment->where('id', $id)->update(['status'=>0]);
            $point = new Point();
            $point->setStatus($comment->user_id, $id, 2, ConstVar::$POINT_STATUS_CANCEL);
            $point->resum($comment->user_id);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500, 50000);
        }
        if($comment->comment_id == 0){
            $sub_count = $postComment->where('comment_id', $id)->where('status', 1)->count();
            $postComment->where('comment_id', $id)->where('status', 1)->update(['status'=>0]);
            $postSta->where('post_id', $comment->post_id)->where('comment', '>', 0)->decrement('comment', $sub_count+1);
        }else{
            $postSta->where('post_id', $comment->post_id)->where('comment', '>', 0)->decrement('comment');
        }
        if($comment->user_id != $user->user_id){
            $msg = Msg::disable_comment($comment->created_at->toDateTimeString());
            dispatch(new SendSystemMsgJob([$comment->user_id], $msg, ConstVar::$IM_MSG_COMMON));
        }
        return $this->sendSuccessResponse([]);
    }

    /**
     * 设置是否能在首页显示
     * @param $id
     * @param Post $post
     * @return \Illuminate\Http\JsonResponse
     */
    public function editPublic($id, Post $post){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $user = Auth::user();
        if(empty($user->is_admin)){
            return $this->sendFailResponse(400, 40101);
        }
        $post = $post->where('id', $id)->first();
        $post->is_public = intval($post->is_public XOR 1);
        $post->save();
        if(!$post->public){
            $msg = Msg::disable_public($post->created_at->toDateTimeString());
            dispatch(new SendSystemMsgJob([$post->user_id], $msg, 0));
        }

        return $this->sendSuccessResponse(['is_public'=>$post->is_public]);
    }
}